package com.sc.auction.auction.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.auction.auction.bean.LikeVo;
import com.sc.auction.auction.pojo.Auction;
import com.sc.auction.auction.pojo.Handsel;
import com.sc.auction.auction.pojo.Userlike;
import com.sc.auction.auction.service.AuctionService;
import com.sc.auction.auction.service.UserlikeService;
import com.sc.auction.common.exception.AuctionException;
import com.sc.auction.common.jwt.JwtUtils;
import com.sc.auction.common.util.R;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lsc
 * @since 2020-09-15
 */
@CrossOrigin
@RestController
@RequestMapping("/auction/userlike")
public class UserlikeController {


    @Autowired
    public UserlikeService userlikeService;

    @Autowired
    public AuctionService auctionService;

    @ApiOperation("插入收藏表")
    @PostMapping("/insertLike")
    public R insertLike(@RequestBody LikeVo likeVo) throws AuctionException {
        System.out.println("charuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu");
        System.out.println(likeVo);
        Userlike like=new Userlike();
        like.setAuctionid(likeVo.getAuctionid());
        Claims claims = JwtUtils.checkJwt(likeVo.getToken());
        Integer id = (Integer)claims.get("id");
        like.setUserid(id);
        QueryWrapper<Userlike> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",id);
        List<Userlike> list = userlikeService.list(queryWrapper);
        for (Userlike u:list
             ) {
            if (u.getAuctionid()==likeVo.getAuctionid()){
                return null;
            }
        }
        if (like.getAuctionid()!=null&&like.getUserid()!=null){
            boolean save = userlikeService.save(like);
            if (save){
                return R.ok().data("save",save);
            }else {
                throw  new AuctionException(201,"收藏失败");
            }
        }
        return R.error();
    }
    @ApiOperation("查询是否已经收藏")
    @PostMapping("/haveLike")
    public  R  haveLie(@RequestBody(required = false) LikeVo likeVo) throws AuctionException {
        System.out.println(likeVo);
        Claims claims = JwtUtils.checkJwt(likeVo.getToken());
        Integer id = (Integer)claims.get("id");
        QueryWrapper<Userlike> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",id);
        queryWrapper.eq("auctionid",likeVo.getAuctionid());
        Userlike one = userlikeService.getOne(queryWrapper);
        if (one!=null){
            return  R.ok();
        }else {
            throw  new  AuctionException(201,"收藏表中不存在该条");
        }
    }

        //查询用户的个人收藏列表
        @ApiOperation("查询收藏列表")
        @GetMapping("/findAllUserLike")
        public R findAllUserLike(@ApiParam(name = "page",defaultValue = "1",required = true)@RequestParam("page") Integer page,
                                 @ApiParam(name = "limit",defaultValue = "5",required = true)@RequestParam("limit") Integer limit,
                                 @ApiParam(name = "status") Integer status,
                                 Integer userId){
            Page<Userlike> pageBean = new Page<>(page,limit);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("userid",userId);
            if(status != null){
                queryWrapper.eq("status",status);
            }
            userlikeService.page(pageBean, queryWrapper);
            List<Userlike> records = pageBean.getRecords();
            List<Userlike> list1 = new ArrayList<>();
            for (Userlike userlike:records) {
                Auction auction = auctionService.getById(userlike.getAuctionid());
                userlike.setGoodsName(auction.getGoodsName());
                list1.add(userlike);
            }
            long total = pageBean.getTotal();
            if(list1!=null && !list1.isEmpty()){
                return R.ok().data("userLike",list1).data("total",total).code(200).message("查询个人收藏列表成功!");
            }else{
                return R.error().code(201).message("失败");
            }
        }

        //删除个人收藏
        @ApiOperation("删除收藏拍品")
        @GetMapping("/deleteUserLike")
        public R deleteUserLike(Integer id){
            boolean b = userlikeService.removeById(id);
            if(b){
                return R.ok().code(200).message("删除收藏信息成功");
            }else{
                return R.error().code(201).message("删除藏品失败");
            }
        }
}

